package com.gugee.tiktok.web.repository.impl;

import com.gugee.tiktok.web.dto.MusicResponse;
import com.gugee.tiktok.web.model.es.ESDayVideoGrowth;
import com.gugee.tiktok.web.model.es.ESVideo;
import com.gugee.tiktok.web.repository.MusicCollectionExtRepository;
import com.xiguaji.boot.toolkit.JsonUtil;
import com.xiguaji.boot.toolkit.jdbc.JdbcHolder;
import com.xiguaji.boot.toolkit.jdbc.Page;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MusicCollectionExtRepositoryImpl implements MusicCollectionExtRepository {

    @Override
    public Page<MusicResponse> getPage(int page, int pageSize, String keyword, Long userId) {
        String selectSql = "select c.id as collectionId,m.* ";
        StringBuilder sql = new StringBuilder(" from music_collection c left join music m on c.mid = m.mid ");
        sql.append("where c.user_id = ? ");
        List<Object> params = new ArrayList<>();
        params.add(userId);

        if(!StringUtils.isEmpty(keyword)){
            sql.append(" and (m.author like ? or m.title like ?) ");
            params.add("%"+keyword+"%");
            params.add("%"+keyword+"%");
        }
        sql.append(" order by c.create_time desc");

        Page<MusicResponse> pageResponse = new Page(null,0,0,0,0);

        int totalRow = JdbcHolder.getJdbcTemplate().queryForObject("select count(*) "+sql.toString(), params.toArray(), Integer.class);

        if(totalRow == 0){
            return pageResponse;
        }
        List<Map<String,Object>> lists = JdbcHolder.getJdbcTemplate().queryForList(selectSql+sql.toString(), params.toArray());
        List<MusicResponse> musicResponses = new ManagedList<>();

        lists.forEach( m -> {
            MusicResponse musicResponse = new MusicResponse();
            musicResponse.setCollectId((Long) m.get("collectionId"));
            musicResponse.setFavorite(true);
            musicResponse.setTitle(m.get("title").toString());
            musicResponse.setAuthor(m.get("author").toString());
            musicResponse.setAuthorDeleted((Boolean) m.get("author_deleted"));
            musicResponse.setCoverMedium(m.get("cover_medium").toString());
            musicResponse.setDuration((Integer) m.get("duration"));
            musicResponse.setId((Long) m.get("id"));
            musicResponse.setMid((Long) m.get("mid"));
            musicResponse.setOfflineDesc((String) m.get("offline_desc"));
            musicResponse.setRegion(m.get("region").toString());
            musicResponse.setPlayUrl(m.get("play_url").toString());
            musicResponse.setShareUrl(m.get("share_url").toString());
            musicResponse.setQrCode(m.get("qr_code") == null? "":m.get("qr_code").toString());
            musicResponse.setUid((Long) m.get("uid"));
            musicResponse.setUserCount((Long) m.get("user_count"));
            musicResponse.setUserGrowth((Long) m.get("user_growth"));

            if(!StringUtils.isEmpty(m.get("day_video_growth"))){
                List<ESDayVideoGrowth> esDayVideoGrowths = JsonUtil.jsonParse(List.class,m.get("day_video_growth").toString());
                musicResponse.setVideoGrowths(esDayVideoGrowths);
            }
            if(!StringUtils.isEmpty(m.get("top_videos"))){
                List<ESVideo> esDayVideoGrowths = JsonUtil.jsonParse(List.class,m.get("top_videos").toString());
                musicResponse.setTopVideos(esDayVideoGrowths);
            }
            musicResponses.add(musicResponse);
        });
        pageResponse = new Page(musicResponses,totalRow,0,0,0);

        return pageResponse;
    }
}
